import pygame

from actor.mine_boom import Mine, Boom
from dialog.tunnel_dialog import TunnelTask2BeginDialog, TunnelTask2EndDialog, TunnelTaskFailDialog
from scene.task import BaseTask, TaskStatus


class KillJapaneseTask(BaseTask):
    def __init__(self, xiao_tie, japanese, font):
        self.japanese = japanese
        self.xiao_tie = xiao_tie
        self.mines = pygame.sprite.Group()
        self.booms = pygame.sprite.Group()

        self.score = 0
        mine = pygame.image.load("./resources/images/mine/mine.png")
        self.mine_image = pygame.transform.scale(mine, (40, 40))
        self.boom_image = pygame.image.load("./resources/images/mine/boom.png")

        self.task_dialog = TunnelTask2BeginDialog(font)
        self.win_dialog = TunnelTask2EndDialog(font)
        self.fail_dialog = TunnelTaskFailDialog(font)

        super(KillJapaneseTask, self).__init__(self.task_dialog, self.win_dialog,
                                               self.fail_dialog)

    def draw_task(self, current_surface, x, y):
        for mine in self.mines:
            mine.draw(current_surface, x, y)

        for boom in self.booms:
            boom.draw(current_surface, x, y)

    def do_self_task(self, key_down, key_list):
        collisions = pygame.sprite.groupcollide(self.mines, self.japanese, True, True)

        for wall in collisions:
            self.__create_boom(wall.rect.x - wall.rect.width,
                               wall.rect.y - wall.rect.height)
            self.score += 1
            if self.score > 2:
                self.task_status = TaskStatus.win_dialog
                # self.xiao_tie.set_status("army")

        if key_down and key_list[pygame.K_SPACE]:
            self.__create_mine(self.xiao_tie.pos_x, self.xiao_tie.pos_y)

        for boom in self.booms:
            if boom.kill_flag():
                self.booms.remove(boom)

    def __create_boom(self, x, y):
        mine = Boom(x, y, self.boom_image, 4, 3)
        self.booms.add(mine)

    def __create_mine(self, x, y):
        mine = Mine(x, y, self.mine_image)
        self.mines.add(mine)
